<template>
    <div class="achievement-list-page">
        <el-card shadow="never" class="filter">
            <el-form
                :model="form"
                label-suffix="："
                label-width="90px"
                label-position="right"
                ref="form"
            >
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="关键词" prop="keyword">
                            <el-input v-model="form.keyword" placeholder class="input-width"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <div class="border-bottom"></div>
                <div class="mt-4 text-center">
                    <el-button type="primary" @click="onSubmit">搜索</el-button>
                    <el-button type="text" @click="onResetFilters">清空筛选条件</el-button>
                </div>
            </el-form>
        </el-card>
        <el-card shadow="never" class="mt-4">
            <el-button type="primary" @click="onAddClick">新增</el-button>
            <div class="mt-3" v-loading="loading">
                <el-table :data="data">
                    <el-table-column label="名称" prop="name"></el-table-column>
                    <el-table-column label="描述" prop="description"></el-table-column>
                    <el-table-column label="成就">
                        <template slot-scope="{row}">
                            <img :src="row.image" class="logo" />
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="{row}">
                            <el-button type="text" @click="onEditClick(row)">编辑</el-button>
                            <el-button type="text" @click="onDeleteClick(row)" class="red-color">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div v-if="totalElements > 0" class="footer">
                <pagination
                    ref="pagination"
                    :currentPage="form.page"
                    :pageSize="form.size"
                    :handleCurrentChange="onCurrentChange"
                    :handlePageSizeChange="onPageSizeChange"
                    :totalElements="totalElements"
                ></pagination>
            </div>
        </el-card>
    </div>
</template>

<script>
import {
    Table,
    TableColumn,
    Input,
    Col,
    Row,
    Form,
    FormItem,
} from 'element-ui';
import Pagination from '../../components/base/Pagination';

export default {
    components: {
        ElInput: Input,
        ElForm: Form,
        ElFormItem: FormItem,
        ElCol: Col,
        ElRow: Row,
        ElTable: Table,
        ElTableColumn: TableColumn,
        Pagination,
    },
    mixins: [require('../../mixins/CacheQuery').default],
    data() {
        return {
            loading: false,
            data: [1],
            form: {
                page: 1,
                size: 25,
                keyword: '',
            },
            totalElements: 0,
        };
    },
    methods: {
        onSubmit() {
            this.form.page = 1;
            this.fetchData();
        },
        onResetFilters() {
            this.$refs.form.resetFields();
        },
        onCurrentChange(val) {
            this.form.page = val;
            this.fetchData();
        },
        onPageSizeChange(val) {
            this.form.size = val;
            this.form.page = 1;
            this.fetchData();
        },
        fetchData() {
            this.loading = true;
            API.get('url', {
                params: {
                    page: this.form.page,
                    size: this.form.size,
                },
            }).then((res) => {
                this.data = res.content;
                this.totalElements = res.pagination.total;
            }).finally(() => {
                this.loading = false;
            });
        },
        onEditClick() {
            this.$router.push({
                name: 'ExampleDetail',
                query: {
                    id: '1',
                },
            });
        },
        onAddClick() {
            this.$router.push({
                name: 'ExampleAdd',
            });
        },
        onDeleteClick() {
            $ele.$confirm('content', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.loading = true;
                API.delete('url', {
                }).then(() => {
                    $ele.$message.success('删除成功');
                }).finally(() => {
                    this.loading = false;
                });
            }).catch(() => { });
        },
    },
    mounted() {
        this.fetchData();
    },
};
</script>

<style lang="scss">
.achievement-list-page {
    .filter {
        .input-width {
            max-width: 300px;
        }
    }
    .logo {
        width: 60px;
        height: 60px;
    }
    .footer {
        height: 44px;
        background: #f7f7f7;
        display: flex;
        justify-content: flex-end;
        align-items: center;
        padding-left: 20px;
        padding-right: 20px;
    }
}
</style>
